using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class CourseAccessor : ICourseRepository
    {
        public override int Insert(Course entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@CourseId", "Course_Insert", entity.CourseId, entity.CategoryId, entity.Name, entity.Description, entity.Body, entity.Picture, entity.IsActive, entity.CreatedById, entity.CreatedBy, entity.IsFree, entity.Price);
            return int.Parse(rs.ToString());
        }
        public override int Update(Course entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Course_Update", entity.CourseId, entity.CategoryId, entity.Name, entity.Description, entity.Body, entity.Picture, entity.IsActive, entity.CreatedById, entity.CreatedBy, entity.SortIndex, entity.IsFree, entity.Price);
            return rs;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Course_Delete", id) > 0;
        }
        public override bool Delete(Course entity)
        {
            return Delete(entity.CourseId);
        }
        public override List<Course> FindAll()
        {
            return CBO.FillCollection<Course>(IDataProvider.Instance.ExecuteReader("Course_FindAll"));
        }
        public override Course FindById(int id)
        {
            return CBO.FillObject<Course>(IDataProvider.Instance.ExecuteReader("Course_FindById", id));
        }
        public override Course Single(int id)
        {
            return FindById(id);
        }
        public override Course FindByName(string name)
        {
            throw new NotImplementedException();
        }
        public override List<Course> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Course_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Course>(reader);
        }
        public override List<Course> FindByKeyword(int pageSize, int pageNum, out int totalRowCount, string keyword)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Course_Search_Paging", pageSize, pageNum, keyword);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Course>(reader);
        }
        public override List<Course> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
            var query = from c in FindAll()
                        select c;
            totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Course_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Course_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Course_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Course_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Course_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Course_RestoreAll");
            return rs > 0;
        }
        public override List<Course> CourseOfPerson(int personId)
        {
            return CBO.FillCollection<Course>(IDataProvider.Instance.ExecuteReader("Course_OfPerson", personId));
        }
    }
}